<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/wellcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片显示区域 -->
    <el-card >
      <!-- 搜索框 -->
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input  placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsData">
            <el-button  slot="append" icon="el-icon-search" @click="getGoodsData()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" plain @click="goAddpage()">添 加 商 品</el-button>
        </el-col>
      </el-row>

      <!-- 商品信息列表 -->
      <el-table :data="goodsList" stripe  border>
        <el-table-column type="index" label="#" width="50px">
        </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" >
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="110px">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="80px">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="160px">
            <template v-slot="scope">
                {{scope.row.add_time | dataForamt}}
            </template>
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template v-slot="scope">
            <el-tooltip content="修改" placement="top" >
              <el-button size="mini" type="primary" icon="el-icon-edit" ></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button size="mini" type="danger" icon="el-icon-delete" @click="deltGood(scope.row.goods_id)" ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5,10, 20, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>

  </div>
</template>

<script>
export default {
    name:'Lists',
    data(){
        return {
            goodsList:[],
            queryInfo:{
                query:'',
                pagenum:1,
                pagesize:10
            },
            total:0
        }
    },
    created(){
        this.getGoodsData();
    },
    methods:{
        // 获取商品信息数据
        async getGoodsData(){
            const {data:res} = await this.axios.get('goods',{params:this.queryInfo});
            if(res.meta.status !==200){
                return this.$message.error('获取商品信息失败了~');
            }
            // console.log(res);
            this.goodsList = res.data.goods;
            this.total = res.data.total;
            this.$message.success('获取商品信息成功了~')
        },
        // 监听pagesize的改变
        handleSizeChange(val) {
            this.queryInfo.pagesize = val;
            this.getGoodsData();
        },
        // 监听页码值的变化
        handleCurrentChange(val) {
            this.queryInfo.pagenum = val;
            this.getGoodsData();
        },
        // 删除分类
        deltGood(id){
            this.$confirm('此操作将永久删除该分类参数, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                const {data:res} = await this.axios.delete(`goods/${id}`);
                if(res.meta.status !==200){
                    return this.$message.error('获取商品信息失败了~');
                }
                this.getGoodsData();
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
            });
        },
        // 添加按钮事件
        goAddpage(){
          this.$router.push('/goods/add')
        }
    }
}
</script>

<style>

</style>